<!DOCTYPE HTML>
<html>

<head>
	<link rel="bookmark"  type="image/x-icon"  href="/img/logo_miccall.png"/>
	<link rel="shortcut icon" href="/img/logo_miccall.png">
	
			    <title>
    Ame雨
    </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="/amelin-ting/css/mic_main.css" />
    <link rel="stylesheet" href="/amelin-ting/css/dropdownMenu.css" />
    <meta name="keywords" content="Ame雨" />
    
    	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	 
    <noscript>
        <link rel="stylesheet" href="/amelin-ting/css/noscript.css" />
    </noscript>
    <style type="text/css">
        body:before {
          content: ' ';
          position: fixed;
          top: 0;
          background: url('/amelin-ting/img/bg.jpg') center 0 no-repeat;
          right: 0;
          bottom: 0;
          left: 0;
          background-size: cover; 
        }
    </style>

			    
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script async type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


    <script src="/amelin-ting/js/jquery.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrollex.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrolly.min.js"></script>
    <script src="/amelin-ting/js/skel.min.js"></script>
    <script src="/amelin-ting/js/util.js"></script>
    <script src="/amelin-ting/js/main.js"></script>
	
<meta name="generator" content="Hexo 5.4.0"></head>
    
		
<!-- Layouts -->



<!--  代码渲染  -->
<link rel="stylesheet" href="/css/prism_coy.css" />
<link rel="stylesheet" href="/css/typo.css" />
<!-- 文章页 -->
<body class="is-loading">
    <!-- Wrapper 外包 s-->
    <div id="wrapper" class="fade-in">
        <!-- Intro 头部显示 s -->
        <!-- Intro 头部显示 e -->
        <!-- Header 头部logo start -->
        <header id="header">
    <a href="/amelin-ting/" class="logo">Art Space</a>
</header>
        <!-- Nav 导航条 start -->
        <nav id="nav" class="special" >
            <ul class="menu links" >
			<!-- Homepage  主页  --> 
			<li >
	            <a href="/amelin-ting/" rel="nofollow">主页</a>
	        </li>
			<!-- categories_name  分类   --> 
	        
	        <li class="active">
	            <a href="#s1">分类</a>
	                    <ul class="submenu">
	                        <li>
	                        <a class="category-link" href="/amelin-ting/categories/Go/">Go</a></li><li><a class="category-link" href="/amelin-ting/categories/django/">django</a></li><li><a class="category-link" href="/amelin-ting/categories/drf/">drf</a></li><li><a class="category-link" href="/amelin-ting/categories/gin/">gin</a></li><li><a class="category-link" href="/amelin-ting/categories/go/">go</a></li><li><a class="category-link" href="/amelin-ting/categories/linux/">linux</a></li><li><a class="category-link" href="/amelin-ting/categories/mysql/">mysql</a></li><li><a class="category-link" href="/amelin-ting/categories/python/">python</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">内网穿透</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%89%8D%E7%AB%AF/">前端</a>
	                    </ul>
	        </li>
	        
	        <!-- archives  归档   --> 
	        
	        
		        <!-- Pages 自定义   -->
		        
		        <li>
		            <a href="/about/" title="简历">
		                简历
		            </a>
		        </li>
		        
		        <li>
		            <a href="/group/" title="团队">
		                团队
		            </a>
		        </li>
		        
		        <li>
		            <a href="/gallery/" title="图库">
		                图库
		            </a>
		        </li>
		        
		        <li>
		            <a href="/tag/" title="标签">
		                标签
		            </a>
		        </li>
		        


            </ul>
            <!-- icons 图标   -->
			<ul class="icons">
                    
                    <li>
                        <a title="github" href="https://github.com/miccall" target="_blank" rel="noopener">
                            <i class="icon fa fa-github"></i>
                        </a>
                    </li>
                    
                    <li>
                        <a title="500px" href="http://500px.com" target="_blank" rel="noopener">
                            <i class="icon fa fa-500px"></i>
                        </a>
                    </li>
                    
			</ul>
</nav>

        <div id="main" >
            <div class ="post_page_title_img" style="height: 25rem;background-image: url(https://img2.baidu.com/it/u=4039018297,468112435&amp;fm=26&amp;fmt=auto);background-position: center; background-repeat:no-repeat; background-size:cover;-moz-background-size:cover;overflow:hidden;" >
                <a href="#" style="padding: 4rem 4rem 2rem 4rem ;"><h2 >cookies&amp;session</h2></a>
            </div>
            <!-- Post -->
            <div class="typo" style="padding: 3rem;">
                <h2 id="一、cookies-amp-session"><a href="#一、cookies-amp-session" class="headerlink" title="一、cookies&amp;session"></a>一、cookies&amp;session</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">发展史</span></span><br><span class="line"><span class="string">	1.网站都没有保存用户功能的需求 所有用户访问返回的结果都是一样的</span></span><br><span class="line"><span class="string">		eg:新闻、博客、文章...</span></span><br><span class="line"><span class="string">	</span></span><br><span class="line"><span class="string">	2.出现了一些需要保存用户信息的网站</span></span><br><span class="line"><span class="string">		eg:淘宝、支付宝、京东...</span></span><br><span class="line"><span class="string">		</span></span><br><span class="line"><span class="string">		以登陆功能为例:如果不保存用户登陆状态 也就意味着用户每次访问网站都需要重复的输入用户名和密码(你觉得这样的网站你还想用吗？)</span></span><br><span class="line"><span class="string">		当用户第一次登陆成功之后 将用户的用户名密码返回给用户浏览器 让用户浏览器保存在本地，之后访问网站的时候浏览器自动将保存在浏览器上的用户名和密码发送给服务端，服务端获取之后自动验证</span></span><br><span class="line"><span class="string">		早起这种方式具有非常大的安全隐患		</span></span><br><span class="line"><span class="string">		</span></span><br><span class="line"><span class="string">		优化:</span></span><br><span class="line"><span class="string">			当用户登陆成功之后，服务端产生一个随机字符串(在服务端保存数据,用kv键值对的形式)，交由客户端浏览器保存</span></span><br><span class="line"><span class="string">			随机字符串1:用户1相关信息</span></span><br><span class="line"><span class="string">			随机字符串2:用户2相关信息</span></span><br><span class="line"><span class="string">			随机字符串3:用户3相关信息</span></span><br><span class="line"><span class="string">			之后访问服务端的时候，都带着该随机字符串，服务端去数据库中比对是否有对应的随机字符串从而获取到对应的用户信息</span></span><br><span class="line"><span class="string">			</span></span><br><span class="line"><span class="string">但是如果你拿到了截获到了该随机字符串，那么你就可以冒充当前用户 其实还是有安全隐患的</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">你要知道在web领域没有绝对的安全也没有绝对的不安全</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="comment"># cookie</span></span><br><span class="line">	服务端保存在客户端浏览器上的信息都可以称之为cookie</span><br><span class="line">  	它的表现形式一般都是k:v键值对(可以有多个)</span><br><span class="line"><span class="comment"># session</span></span><br><span class="line">	数据是保存在服务端的并且它的表现形式一般也是k:v键值对(可以有多个)</span><br><span class="line">    </span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">下述内容暂时了解即可 先给我搞明白最简单的cookie与session使用再说话！</span></span><br><span class="line"><span class="string">token</span></span><br><span class="line"><span class="string">	session虽然数据是保存在服务端的 但是禁不住数据量大</span></span><br><span class="line"><span class="string">  服务端不再保存数据</span></span><br><span class="line"><span class="string">  	登陆成功之后 将一段用户信息进行加密处理(加密算法之后你公司开发知道)</span></span><br><span class="line"><span class="string">    将加密之后的结果拼接在信息后面 整体返回给浏览器保存 </span></span><br><span class="line"><span class="string">    浏览器下次访问的时候带着该信息 服务端自动切去前面一段信息再次使用自己的加密算法</span></span><br><span class="line"><span class="string">    跟浏览器尾部的密文进行比对jwt认证三段信息</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span>    </span><br><span class="line"></span><br><span class="line">	</span><br><span class="line">总结:</span><br><span class="line">  	<span class="number">1.</span>cookie就是保存在客户端浏览器上的信息</span><br><span class="line">    <span class="number">2.</span>session就是保存在服务端上的信息</span><br><span class="line">    <span class="number">3.</span>session是基于cookie工作的(其实大部分的保存用户状态的操作都需要使用到cookie)</span><br></pre></td></tr></table></figure>

<h2 id="二、Django操作cookies"><a href="#二、Django操作cookies" class="headerlink" title="二、Django操作cookies"></a>二、Django操作cookies</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 虽然cookie是服务端告诉客户端浏览器需要保存内容</span></span><br><span class="line"><span class="comment"># 但是客户端浏览器可以选择拒绝保存 如果禁止了 那么 只要是需要记录用户状态的网站登陆功能都无法使用了</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 视图函数的返回值</span></span><br><span class="line">    <span class="keyword">return</span> HttpResponse()</span><br><span class="line">    <span class="keyword">return</span> render()</span><br><span class="line">    <span class="keyword">return</span> redirect()</span><br><span class="line"></span><br><span class="line">    obj1 = HttpResponse()</span><br><span class="line"><span class="comment"># 操作cookie</span></span><br><span class="line">	<span class="keyword">return</span> obj1</span><br><span class="line"></span><br><span class="line">	obj2 = render()</span><br><span class="line"><span class="comment"># 操作cookie</span></span><br><span class="line">	<span class="keyword">return</span> obj2</span><br><span class="line"></span><br><span class="line">	obj3 = redirect()</span><br><span class="line"><span class="comment"># 操作cookie</span></span><br><span class="line">	<span class="keyword">return</span> obj3</span><br><span class="line"><span class="comment"># 如果你想要操作cookie，你就不得不利用obj对象</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">设置cookie</span></span><br><span class="line"><span class="string">	obj.set_cookie(key,value)</span></span><br><span class="line"><span class="string">获取cookie</span></span><br><span class="line"><span class="string">	request.COOKIES.get(key)</span></span><br><span class="line"><span class="string">在设置cookie的时候可以添加一个超时时间</span></span><br><span class="line"><span class="string">	obj.set_cookie(&#x27;username&#x27;, &#x27;jason666&#x27;,max_age=3,expires=3)</span></span><br><span class="line"><span class="string">	</span></span><br><span class="line"><span class="string">	max_age</span></span><br><span class="line"><span class="string">	expires</span></span><br><span class="line"><span class="string">		两者都是设置超时时间的 并且都是以秒为单位</span></span><br><span class="line"><span class="string">		需要注意的是 针对IE浏览器需要使用expires</span></span><br><span class="line"><span class="string">主动删除cookie(注销功能)</span></span><br><span class="line"><span class="string">	</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="comment"># 我们完成一个真正的登陆功能</span></span><br><span class="line"><span class="comment"># 校验用户是否登陆的装饰器</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">用户如果在没有登陆的情况下想访问一个需要登陆的页面</span></span><br><span class="line"><span class="string">那么先跳转到登陆页面 当用户输入正确的用户名和密码之后</span></span><br><span class="line"><span class="string">应该跳转到用户之前想要访问的页面去 而不是直接写死</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 登录认证装饰器</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">login_auth</span>(<span class="params">func</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">inner</span>(<span class="params">request,*args,**kwargs</span>):</span></span><br><span class="line">        <span class="comment"># print(request.path_info)</span></span><br><span class="line">        <span class="comment"># print(request.get_full_path())  # 能够获取到用户上一次想要访问的url</span></span><br><span class="line">        target_url = request.get_full_path()</span><br><span class="line">        <span class="keyword">if</span> request.COOKIES.get(<span class="string">&#x27;username&#x27;</span>):</span><br><span class="line">            <span class="keyword">return</span> func(request,*args,**kwargs)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">return</span> redirect(<span class="string">&#x27;/login/?next=%s&#x27;</span>%target_url)</span><br><span class="line">    <span class="keyword">return</span> inner</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">login</span>(<span class="params">request</span>):</span></span><br><span class="line">    <span class="keyword">if</span> request.method == <span class="string">&#x27;POST&#x27;</span>:</span><br><span class="line">        username = request.POST.get(<span class="string">&#x27;username&#x27;</span>)</span><br><span class="line">        password = request.POST.get(<span class="string">&#x27;password&#x27;</span>)</span><br><span class="line">        <span class="keyword">if</span> username == <span class="string">&#x27;jason&#x27;</span> <span class="keyword">and</span> password == <span class="string">&#x27;123&#x27;</span>:</span><br><span class="line">            <span class="comment"># 获取用户上一次想要访问的url</span></span><br><span class="line">            target_url = request.GET.get(<span class="string">&#x27;next&#x27;</span>)  <span class="comment"># 这个结果可能是None</span></span><br><span class="line">            <span class="keyword">if</span> target_url:</span><br><span class="line">                obj = redirect(target_url)</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                <span class="comment"># 保存用户登陆状态</span></span><br><span class="line">                obj = redirect(<span class="string">&#x27;/home/&#x27;</span>)</span><br><span class="line">            <span class="comment"># 让浏览器记录cookie数据</span></span><br><span class="line">            obj.set_cookie(<span class="string">&#x27;username&#x27;</span>, <span class="string">&#x27;jason666&#x27;</span>)</span><br><span class="line">            <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">            浏览器不单单会帮你存</span></span><br><span class="line"><span class="string">            而且后面每次访问你的时候还会带着它过来</span></span><br><span class="line"><span class="string">            &quot;&quot;&quot;</span></span><br><span class="line">            <span class="comment"># 跳转到一个需要用户登陆之后才能看的页面</span></span><br><span class="line">            <span class="keyword">return</span> obj</span><br><span class="line">    <span class="keyword">return</span> render(request,<span class="string">&#x27;login.html&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@login_auth</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">home</span>(<span class="params">request</span>):</span></span><br><span class="line">    <span class="comment"># 获取cookie信息 判断你有没有</span></span><br><span class="line">    <span class="keyword">if</span> request.COOKIES.get(<span class="string">&#x27;username&#x27;</span>) == <span class="string">&#x27;jason666&#x27;</span>:</span><br><span class="line">        <span class="keyword">return</span> HttpResponse(<span class="string">&quot;我是home页面，只有登陆的用户才能进来哟~&quot;</span>)</span><br><span class="line">    <span class="comment"># 没有登陆应该跳转到登陆页面</span></span><br><span class="line">    <span class="keyword">return</span> redirect(<span class="string">&#x27;/login/&#x27;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="三、Django操作session"><a href="#三、Django操作session" class="headerlink" title="三、Django操作session"></a>三、Django操作session</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 代码演示</span><br><span class="line">    <span class="comment"># views</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">set_session</span>(<span class="params">request</span>):</span></span><br><span class="line">        request.session[<span class="string">&#x27;hobby&#x27;</span>] = <span class="string">&#x27;girl&#x27;</span></span><br><span class="line">        <span class="comment"># request.session[&#x27;hobby1&#x27;] = &#x27;girl1&#x27;</span></span><br><span class="line">        <span class="comment"># request.session[&#x27;hobby2&#x27;] = &#x27;girl2&#x27;</span></span><br><span class="line">        <span class="comment"># request.session[&#x27;hobby3&#x27;] = &#x27;girl3&#x27;</span></span><br><span class="line">        <span class="comment"># request.session.set_expiry()</span></span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        内部发送了那些事</span></span><br><span class="line"><span class="string">            1.django内部会自动帮你生成一个随机字符串</span></span><br><span class="line"><span class="string">            2.django内部自动将随机字符串和对应的数据存储到django_session表中</span></span><br><span class="line"><span class="string">                2.1先在内存中产生操作数据的缓存</span></span><br><span class="line"><span class="string">                2.2在响应结果django中间件的时候才真正的操作数据库</span></span><br><span class="line"><span class="string">            3.将产生的随机字符串返回给客户端浏览器保存</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        <span class="keyword">return</span> HttpResponse(<span class="string">&#x27;嘿嘿嘿&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">get_session</span>(<span class="params">request</span>):</span></span><br><span class="line">        <span class="comment"># print(request.session.get(&#x27;hobby&#x27;))</span></span><br><span class="line">        <span class="keyword">if</span> request.session.get(<span class="string">&#x27;hobby&#x27;</span>):</span><br><span class="line">            <span class="built_in">print</span>(request.session)</span><br><span class="line">            <span class="built_in">print</span>(request.session.get(<span class="string">&quot;hobby&quot;</span>))</span><br><span class="line">            <span class="built_in">print</span>(request.session.get(<span class="string">&quot;hobby1&quot;</span>))</span><br><span class="line">            <span class="built_in">print</span>(request.session.get(<span class="string">&quot;hobby2&quot;</span>))</span><br><span class="line">            <span class="built_in">print</span>(request.session.get(<span class="string">&quot;hobby3&quot;</span>))</span><br><span class="line"></span><br><span class="line">            <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">            内部发送了那些事</span></span><br><span class="line"><span class="string">                1.自动从浏览器请求中获取sessionid对应的随机字符串</span></span><br><span class="line"><span class="string">                2.拿着该随机字符串去django_session表中查找对应的数据</span></span><br><span class="line"><span class="string">                3.</span></span><br><span class="line"><span class="string">                    如果比对上了 则将对应的数据取出并以字典的形式封装到request.session中</span></span><br><span class="line"><span class="string">                    如果比对不上 则request.session.get()返回的是None</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">            &quot;&quot;&quot;</span></span><br><span class="line">            <span class="keyword">return</span> HttpResponse(<span class="string">&quot;哈哈哈&quot;</span>)</span><br><span class="line">        <span class="keyword">return</span> HttpResponse(<span class="string">&quot;大爷 关门了 明晚再来吧&quot;</span>)</span><br><span class="line">    </span><br><span class="line"><span class="number">2.</span> 进一步解释说明</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    session数据是保存在服务端的(存？)，给客户端返回的是一个随机字符串</span></span><br><span class="line"><span class="string">        sessionid:随机字符串</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    在默认情况下操作session的时候需要django默认的一张django_session表</span></span><br><span class="line"><span class="string">        数据库迁移命令</span></span><br><span class="line"><span class="string">            django会自己创建很多表	django_session就是其中的一张</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    django默认session的过期时间是14天</span></span><br><span class="line"><span class="string">        但是你也可以人为的修改它</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    设置session	</span></span><br><span class="line"><span class="string">    request.session[&#x27;key&#x27;] = value</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    获取session</span></span><br><span class="line"><span class="string">    request.session.get(&#x27;key&#x27;)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    设置过期时间</span></span><br><span class="line"><span class="string">    request.session.set_expiry()</span></span><br><span class="line"><span class="string">        括号内可以放四种类型的参数</span></span><br><span class="line"><span class="string">            1.整数						多少秒</span></span><br><span class="line"><span class="string">            2.日期对象			   到指定日期就失效</span></span><br><span class="line"><span class="string">            3.0								一旦当前浏览器窗口关闭立刻失效</span></span><br><span class="line"><span class="string">            4.不写						失效时间就取决于django内部全局session默认的失效时间</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    清除session	</span></span><br><span class="line"><span class="string">        request.session.delete()  # 只删服务端的 客户端的不删</span></span><br><span class="line"><span class="string">        request.session.flush()  # 浏览器和服务端都清空(推荐使用)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    session是保存在服务端的 但是session的保存位置可以有多种选择</span></span><br><span class="line"><span class="string">        1.MySQL</span></span><br><span class="line"><span class="string">        2.文件</span></span><br><span class="line"><span class="string">        3.redis</span></span><br><span class="line"><span class="string">        4.memcache</span></span><br><span class="line"><span class="string">        ...</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    django_session表中的数据条数是取决于浏览器的</span></span><br><span class="line"><span class="string">        同一个计算机上(IP地址)同一个浏览器只会有一条数据生效</span></span><br><span class="line"><span class="string">        (当session过期的时候可能会出现多条数据对应一个浏览器，但是该现象不会持续很久，内部会自动识别过期的数据清除 你也可以通过代码清除)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">        主要是为了节省服务端数据库资源</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 利用session实现登陆验证</span></span><br></pre></td></tr></table></figure>

<h2 id="四、CBV添加装饰器"><a href="#四、CBV添加装饰器" class="headerlink" title="四、CBV添加装饰器"></a>四、CBV添加装饰器</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 代码演示</span><br><span class="line">	<span class="keyword">from</span> django.views <span class="keyword">import</span> View</span><br><span class="line">	<span class="keyword">from</span> django.utils.decorators <span class="keyword">import</span> method_decorator</span><br><span class="line"><span class="meta">	@method_decorator(<span class="params">input_wrapper, name=<span class="string">&#x27;get&#x27;</span></span>)  </span><span class="comment"># 方式2(可以添加多个针对不同的方法加不同的装饰器)</span></span><br><span class="line"><span class="meta">    @method_decorator(<span class="params">input_wrapper, name=<span class="string">&#x27;post&#x27;</span></span>)</span></span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">MyViews</span>(<span class="params">View</span>):</span></span><br><span class="line"><span class="meta">        @method_decorator(<span class="params">input_wrapper</span>)  </span><span class="comment"># 方式三 这是利用了View源码的特征 会给类所有的方法加上装饰器</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">dispatch</span>(<span class="params">self, request, *args, **kwargs</span>):</span></span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">super</span>().dispatch(request, *args, **kwargs)</span><br><span class="line"></span><br><span class="line"><span class="meta">        @method_decorator(<span class="params">input_wrapper</span>)  </span><span class="comment"># 第一种方式 指名道姓</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">get</span>(<span class="params">self</span>):</span></span><br><span class="line">            <span class="keyword">return</span> HttpResponse(<span class="string">&#x27;get&#x27;</span>)</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">post</span>(<span class="params">self</span>):</span></span><br><span class="line">            <span class="keyword">return</span> HttpResponse(<span class="string">&#x27;post&#x27;</span>)</span><br><span class="line"><span class="number">2.</span> 解释说明</span><br><span class="line">	<span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">	CBV想要加上装饰器得导入模块  </span></span><br><span class="line"><span class="string">	from django.utils.decorators import method_decorator</span></span><br><span class="line"><span class="string">	要不然普通的加装饰器会报错</span></span><br><span class="line"><span class="string">	其实CBV是不建议你直接加装饰器，哪怕成功加上了,也不建议</span></span><br><span class="line"><span class="string">	&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>


            </div>

            <!-- Post Comments -->
            
    <!-- 使用 DISQUS_CLICK -->
<div id="disqus-comment">
    <div id="disqus_thread"></div>

<!-- add animation -->
<style>
	.disqus_click_btn {
            line-height: 30px;
            margin: 0;
            min-width: 50px;
            padding: 0 14px;
            display: inline-block;
            font-family: "Roboto", "Helvetica", "Arial", sans-serif;
            font-size: 14px;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0;
            overflow: hidden;
            will-change: box-shadow;
            transition: box-shadow .2s cubic-bezier(.4, 0, 1, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), color .2s cubic-bezier(.4, 0, .2, 1);
            outline: 0;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            vertical-align: middle;
            border: 0;
            background: rgba(158, 158, 158, .2);
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
            color: #fff;
            background-color: #7EC0EE;
            text-shadow: 0
        }
</style>
	
<div class="btn_click_load" id="disqus_bt"> 
    <button class="disqus_click_btn">点击查看评论</button>
</div>

<!--
<script type="text/javascript">
$('.btn_click_load').click(function() {
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'http-miccall-tech'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    document.getElementById('disqus_bt').style.display = "none";
});
</script>
-->
<script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://gitee.com/AmeX/amelin-ting.git/2018/05/08/Django------cookies&amp;session/';  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'https://gitee.com/AmeX/amelin-ting.git/2018/05/08/Django------cookies&amp;session/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>

<script type="text/javascript">
    $('.btn_click_load').click(function() {  //click to load comments
        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document;
            var s = d.createElement('script');
            s.src = '//http-miccall-tech.disqus.com/embed.js';
            s.setAttribute('data-timestamp', + new Date());
            (d.head || d.body).appendChild(s);
        })();
        $('.btn_click_load').css('display','none');
    });
</script>
</div>
<style>
    #disqus-comment{
        background-color: #eee;
        padding: 2pc;
    }
</style>


        </div>
        <!-- Copyright 版权 start -->
                <div id="copyright">
            <ul>
                <li>&copy;Powered By <a target="_blank" rel="noopener" href="https://hexo.io/zh-cn/" style="border-bottom: none;">hexo</a></li>
                <li>Design: <a target="_blank" rel="noopener" href="http://miccall.tech " style="border-bottom: none;">miccall</a></li>
            </ul>
            
                <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
			
        </div>
    </div>
</body>



 	
</html>
